package com.fqgj.sentry.controller.antiFraud.response.anti.relation;

import com.fqgj.common.utils.CollectionUtils;

import com.fqgj.sentry.biz.antiFraud.domain.OrderVo;
import com.fqgj.sentry.biz.antiFraud.domain.PropertyVo;
import com.fqgj.sentry.controller.antiFraud.response.anti.AntiFraudDataUtil;
import com.fqgj.sentry.controller.antiFraud.response.anti.OrderInfo;
import com.google.gson.Gson;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liupengpeng on 2017/10/17.
 */
public class NodeLinkDataUtil implements Serializable {
    /**
     * * 画订单--属性--订单
     */
    public static NodeLinkInfo drawOrderPropertyOrder(OrderVo orderVo) {
        NodeLinkInfo nodeLinkInfo = new NodeLinkInfo();
        List<String> nodeNum = new ArrayList<>();
        List<Node> nodeList = new ArrayList<>();
        List<Link> linkList = new ArrayList<>();

        //节点数据
        if (!nodeNum.contains(orderVo.getNo())) {
            nodeList.add(new Node(orderVo.getRealName() + "(" + orderVo.getApplyDate() + ")",
                    2, getOrderInfo(orderVo)));
            nodeNum.add(orderVo.getNo());
        }
        List<PropertyVo> propertyVoList = orderVo.getPropertyVoList();
        if (CollectionUtils.isNotEmpty(propertyVoList)) {
            for (PropertyVo propertyVo : propertyVoList) {
                String lineColor = propertyVo.getPropertyTypeEnums().getLineColor();
                String propertyDesc = propertyVo.getPropertyTypeEnums().getDesc();
                //节点
                nodeList.add(new Node(propertyDesc, 1, null));
                nodeNum.add(orderVo.getNo());
                //线
                linkList.add(new Link(orderVo.getNo(), propertyVo.getNo(), lineColor));
                List<OrderVo> propertyOrderList = propertyVo.getOrderVoList();
                for (OrderVo propertyOrder : propertyOrderList) {
                    //节点
                    if (!nodeNum.contains(propertyOrder.getNo())) {
                        nodeList.add(new Node(propertyOrder.getRealName() + "(" + propertyOrder.getApplyDate() + ")",
                                2, getOrderInfo(orderVo)));
                        nodeNum.add(propertyOrder.getNo());
                    }
                    //线
                    linkList.add(new Link(propertyVo.getNo(), propertyOrder.getNo(), lineColor));

                    //重复画订单--属性--订单
                    secondDrawOrderPropertyOrder(nodeList, linkList, nodeNum, propertyOrder);
                }
            }
        }

        nodeLinkInfo.setLinkList(linkList).setNodeList(nodeList);

        return nodeLinkInfo;
    }

    private static OrderInfo getOrderInfo(OrderVo order) {
        OrderInfo orderInfo = new OrderInfo();
        BeanUtils.copyProperties(order, orderInfo);
        return orderInfo;
    }

    /**
     * * 画订单--属性--订单
     */
    public static void secondDrawOrderPropertyOrder(List<Node> nodeList, List<Link> linkList, List<String> nodeNum, OrderVo orderVo) {
        //节点数据
        if (!nodeNum.contains(orderVo.getNo())) {
            nodeList.add(new Node(orderVo.getRealName() + "(" + orderVo.getApplyDate() + ")",
                    2, getOrderInfo(orderVo)));
            nodeNum.add(orderVo.getNo());
        }

        List<PropertyVo> propertyVoList = orderVo.getPropertyVoList();
        if (CollectionUtils.isNotEmpty(propertyVoList)) {
            for (PropertyVo propertyVo : propertyVoList) {
                String lineColor = propertyVo.getPropertyTypeEnums().getLineColor();
                String propertyDesc = propertyVo.getPropertyTypeEnums().getDesc();
                //节点
                nodeList.add(new Node(propertyDesc, 1, null));
                nodeNum.add(orderVo.getNo());
                //线
                linkList.add(new Link(orderVo.getNo(), propertyVo.getNo(), lineColor));
                List<OrderVo> propertyOrderList = propertyVo.getOrderVoList();
                for (OrderVo propertyOrder : propertyOrderList) {
                    //节点
                    if (!nodeNum.contains(propertyOrder.getNo())) {
                        nodeList.add(new Node(propertyOrder.getRealName() + "(" + propertyOrder.getApplyDate() + ")",
                                2, getOrderInfo(orderVo)));
                        nodeNum.add(propertyOrder.getNo());
                    }
                    //线
                    linkList.add(new Link(propertyVo.getNo(), propertyOrder.getNo(), lineColor));
                }
            }
        }

    }


    public static void main(String[] args) {
        OrderVo orderVo = AntiFraudDataUtil.getAntiFraudVo();
        NodeLinkInfo nodeLinkInfo = NodeLinkDataUtil.drawOrderPropertyOrder(orderVo);
        System.out.println(new Gson().toJson(nodeLinkInfo));
    }
}
